package com.vise.bledemo.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vise.bledemo.R;


/**
 * 发料标题  蓝色文字的
 */
public class CompanyCustomView2 extends LinearLayout {


    public EditText cc2_edit1;


    public TextView ccly2_tv1;

    public CompanyCustomView2(Context context) {
        super(context);
        initView();
    }

    public CompanyCustomView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CompanyCustomView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    View view = null;

    private SearchCall searchCall = null;
    private String searchKey = "";
    private Handler taskHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            //收到消息
            if (searchCall != null) {
                searchCall.searchCallString(searchKey);
            }
            return false;
        }
    });

    private CompanyCustomView2 initView() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.company_custom_ly2, null);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        cc2_edit1 = view.findViewById(R.id.cc2_edit1);
        ccly2_tv1 = view.findViewById(R.id.ccly2_tv1);
        addView(view);
        initEditView();
        return this;
    }

    public void setSearchCall(SearchCall searchCall) {
        ccly2_tv1.setVisibility(GONE);
        this.searchCall = searchCall;
    }

    private void initEditView() {
        cc2_edit1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //每次内容改变时，先移除前面的消息，避免太过频繁的搜索
                taskHandler.removeMessages(1);
                searchKey = s.toString();
                //间隔500后发送消息
                taskHandler.sendEmptyMessageDelayed(1, 500);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public interface SearchCall {
        void searchCallString(String key);
    }

}
